<?php

namespace App\Modules\Manage\Model;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Cache;

class SuccessCaseModel extends Model
{
    protected $table = 'success_case';
    protected $fillable = ['uid','username','title','url','pic','cate_id','pub_uid','view_count','created_at','updated_at','type','desc'];
    //public  $timestamps = true;

    
    static function getSuccessCaseListByUid($uid,$merge=array())
    {
        $successCaseList = SuccessCaseModel::whereRaw('1 = 1');
        if(isset($merge['title'])){
            $successCaseList = $successCaseList->where('success_case.title','like','%'.$merge['title'].'%');
        }
        $successCaseList = $successCaseList->where('success_case.uid',$uid)->where('success_case.type',1)
            ->leftJoin('cate','cate.id','=','success_case.cate_id')
            ->select('success_case.*','cate.name')
            ->orderBy('success_case.created_at','DESC')
            ->paginate(5);
        return$successCaseList;
    }

    
    static function getSuccessInfoById($id)
    {
        $successInfo = SuccessCaseModel::where('id',$id)->first();
        return $successInfo;
    }

    
    static function getOtherSuccessByUid($uid,$id,$limit=5)
    {
        $successCaseList = SuccessCaseModel::where('success_case.uid',$uid)->where('success_case.type',1)
            ->where('success_case.id','!=',$id)
            ->leftJoin('cate','cate.id','=','success_case.cate_id')
            ->select('success_case.*','cate.name')
            ->orderBy('success_case.created_at','DESC')
            ->limit($limit)->get()->toArray();
        return $successCaseList;
    }

    static function getSuccessCase()
    {
        if(Cache::has('successCase'))
        {
            $successCase = Cache::get('successCase');
        }
        else
        {
            $successCase = self::select('*')->orderBy('created_at','desc')->get();
            if(!empty($successCase))
            {
                $successCase = $successCase->toArray();
            }
            else
            {
                $successCase = [];
            }
            Cache::put('successCase', $successCase, 60*24);
        }
        return $successCase;
    }

}
